package com.homedo.odin.solution.controller.mng;

import com.homedo.core.common.base.dto.Meta;
import com.homedo.core.common.base.dto.ResultData;
import com.homedo.core.common.exception.BizException;
import com.homedo.odin.solution.model.entity.ProductReq;
import com.homedo.odin.solution.model.request.mng.MngProductOperatorRequestBean;
import com.homedo.odin.solution.model.request.mng.MngProductReqsListRequestBean;
import com.homedo.odin.solution.model.response.mng.MngProductReqPageResponseBean;
import com.homedo.odin.solution.service.ProductReqServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: zhanglei
 * @date: 2018/4/25
 * @Description: PC 活动接口 API
 */
@Api(description = "需求", tags = {"Requirement"}, produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequestMapping("/mng/reqs")
public class MngRequirementController {

    @Autowired
    private ProductReqServiceImpl productReqService;

    @ApiOperation(value = "Mng_产品资料需求列表", httpMethod = "GET", response = ProductReq.class)
    @GetMapping(value = "/products")
    public ResultData productReqList(@Valid @ModelAttribute MngProductReqsListRequestBean requestBean,
            BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultData
                    .newInstance(new Meta(Meta.CODE_451.getCode(), bindingResult.getFieldError().getDefaultMessage()));
        }
        MngProductReqPageResponseBean responseBean = productReqService.productReqList(requestBean);
        return ResultData.newInstance(Meta.CODE_200, responseBean);
    }

    @ApiOperation(value = "Mng_添加跟踪记录", httpMethod = "POST")
    @PostMapping(value = "/products/{reqId}")
    public ResultData operatorProductReq(@PathVariable("reqId") Long reqId,
            @Valid @RequestBody MngProductOperatorRequestBean requestBean, BindingResult bindingResult) {
        if (reqId < 0) {
            throw new BizException(Meta.CODE_451.getCode(), "reqId参数非法");
        }

        if (bindingResult.hasErrors()) {
            return ResultData
                    .newInstance(new Meta(Meta.CODE_451.getCode(), bindingResult.getFieldError().getDefaultMessage()));
        }

        productReqService.operatorProductReq(reqId, requestBean);
        return ResultData.newInstance(Meta.CODE_200, null);
    }
}
